﻿using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Hont.HyperWigglePackage.Editor
{
    public partial class HyperWiggleEditorView
    {
        const float TIMELINE_HEIGHT = 53;
        const float TIMELINE_FRAME_SLOT_WIDTH = 40;
        const float TIMELINE_FRAME_SLOT_HEIGHT = 15;

        const float TIMELINE_ORIGIN_Y = PREVIEW_HEIGHT + LARGE_SPACING;

        Vector2 mTimelineScroll;
        TimelineVO mTimelineVO;
        bool mTimelineFrameBarDrag;

        public TrackVO[] TracksArray { get { return mTimelineVO.TracksArray; } }
        public Color[] FrameColorsArray { get { return mTimelineVO.ColorsArray; } }


        void InitTimelinePartial()
        {
            mTimelineVO = new TimelineVO();
        }

        public void UpdateTimeline(TimelineVO timelineVO)
        {
            mTimelineVO = timelineVO;
        }

        void RefreshTimeline()
        {
            GUI.Box(new Rect(0, TIMELINE_ORIGIN_Y, Screen.width, TIMELINE_HEIGHT + 1), "");

            EditorGUI.IntField(new Rect(
                0,
                TIMELINE_ORIGIN_Y + 12,
                TIMELINE_FRAME_SLOT_WIDTH,
                TIMELINE_FRAME_SLOT_HEIGHT)
                , 0);

            var endFrame = EditorGUI.IntField(new Rect(
                Screen.width - TIMELINE_FRAME_SLOT_WIDTH,
                TIMELINE_ORIGIN_Y + 12,
                TIMELINE_FRAME_SLOT_WIDTH,
                TIMELINE_FRAME_SLOT_HEIGHT)
                , mTimelineVO.EndFrame);

            if (mTimelineVO.EndFrame != endFrame)
            {
                if (mTimelineVO.OnChangeEndFrameCB != null)
                    mTimelineVO.OnChangeEndFrameCB(endFrame);
            }

            var rect = new Rect(TIMELINE_FRAME_SLOT_WIDTH,
                TIMELINE_ORIGIN_Y,
                Screen.width - TIMELINE_FRAME_SLOT_WIDTH * 2,
                TIMELINE_HEIGHT);

            GUI.Box(rect, "");

            var viewRect = new Rect(0, 0, endFrame * (TIMELINE_FRAME_WIDTH + SMALL_SPACING) + 40, 10);
            mTimelineScroll = GUI.BeginScrollView(rect, mTimelineScroll, viewRect, true, false);
            var innerRect = new Rect(0, 0, rect.width, rect.height);
            DrawTimelineFrameGUI(innerRect);

            RefreshFrameBar(rect);
            GUI.EndScrollView();

            if (endFrame != mTimelineVO.EndFrame)
                mTimelineVO.OnChangeFrameCB(endFrame);
        }

        void RefreshFrameBar(Rect parentRect)
        {
            var originX = MID_SPACING;
            var localMousePosition = mMousePosition - parentRect.min + mTimelineScroll;

            var currentX = originX
                + mTimelineVO.CurrentFrame * (TIMELINE_FRAME_WIDTH + SMALL_SPACING)
                + TIMELINE_FRAME_WIDTH * 0.3f;

            var frameBarRect = new Rect(currentX, 0, 5, TIMELINE_FRAME_HEIGHT);
            GUI.Box(frameBarRect, "");

            #region --- Drag ---
            if (frameBarRect.Contains(localMousePosition)
                && !mTimelineFrameBarDrag
                && mIsMouseDown)
            {
                mTimelineFrameBarDrag = true;
            }

            if (mTimelineFrameBarDrag && mIsMouseUpOnce)
                mTimelineFrameBarDrag = false;

            if (mTimelineFrameBarDrag)
            {
                for (int i = 0; i < mTimelineVO.EndFrame; i++)
                {
                    currentX = originX
                       + i * (TIMELINE_FRAME_WIDTH + SMALL_SPACING);

                    var frameRect = new Rect(currentX, 0, TIMELINE_FRAME_WIDTH, TIMELINE_FRAME_HEIGHT);

                    if (frameRect.Contains(localMousePosition)
                        && mTimelineVO.CurrentFrame != i)
                    {
                        GUI.FocusControl("");

                        if (mTimelineVO.OnChangeFrameCB != null)
                            mTimelineVO.OnChangeFrameCB(i);

                        break;
                    }
                }
            }
            #endregion

            #region --- Click ---
            for (int i = 0; i < mTimelineVO.EndFrame; i++)
            {
                currentX = originX
                   + i * (TIMELINE_FRAME_WIDTH + SMALL_SPACING);

                var frameRect = new Rect(currentX, 0, TIMELINE_FRAME_WIDTH, TIMELINE_FRAME_HEIGHT);

                if (frameRect.Contains(localMousePosition)
                    && mTimelineVO.CurrentFrame != i
                    && mIsMouseDownOnce)
                {
                    GUI.FocusControl("");

                    if (mTimelineVO.OnChangeFrameCB != null)
                        mTimelineVO.OnChangeFrameCB(i);

                    break;
                }
            }
            #endregion
        }
    }
}
